package com.zuo_book.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zuo_book.common.Result;
import com.zuo_book.common.SecurityUtils;
import com.zuo_book.entity.SysProjectStage;
import com.zuo_book.service.SysProjectMemberService;
import com.zuo_book.service.SysProjectStageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目阶段控制器
 */
@Slf4j
@RestController
@RequestMapping("/project/stage")
@RequiredArgsConstructor
public class SysProjectStageController {

    private final SysProjectStageService projectStageService;
    private final SysProjectMemberService projectMemberService;

    /**
     * 创建项目阶段
     */
    @PostMapping("/create")
    public Result<Long> createStage(@RequestBody SysProjectStage stage) {
        // 检查当前用户是否有权限创建阶段
        Long currentUserId = SecurityUtils.getCurrentUserId();
        Integer role = projectMemberService.getMemberRole(stage.getProjectId(), currentUserId);
        if (role == null) {
            return Result.fail("您不是该项目的成员");
        }

        Long stageId = projectStageService.createStage(stage);
        return Result.success(stageId);
    }

    /**
     * 更新项目阶段
     */
    @PutMapping("/update")
    public Result<Void> updateStage(@RequestBody SysProjectStage stage) {
        // 获取阶段信息
        SysProjectStage existingStage = projectStageService.getStageById(stage.getId());

        // 检查当前用户是否有权限更新阶段
        Long currentUserId = SecurityUtils.getCurrentUserId();
        Integer role = projectMemberService.getMemberRole(existingStage.getProjectId(), currentUserId);
        if (role == null) {
            return Result.fail("您不是该项目的成员");
        }

        projectStageService.updateStage(stage);
        return Result.success();
    }

    /**
     * 删除项目阶段
     */
    @DeleteMapping("/delete/{stageId}")
    public Result<Void> deleteStage(@PathVariable Long stageId) {
        // 获取阶段信息
        SysProjectStage stage = projectStageService.getStageById(stageId);

        // 检查当前用户是否有权限删除阶段
        Long currentUserId = SecurityUtils.getCurrentUserId();
        Integer role = projectMemberService.getMemberRole(stage.getProjectId(), currentUserId);
        if (role == null || role != 0) {
            return Result.fail("只有项目创建者才能删除阶段");
        }

        projectStageService.deleteStage(stageId);
        return Result.success();
    }

    /**
     * 获取项目阶段详情
     */
    @GetMapping("/info/{stageId}")
    public Result<SysProjectStage> getStageInfo(@PathVariable Long stageId) {
        SysProjectStage stage = projectStageService.getStageById(stageId);

        // 检查当前用户是否是项目成员
        Long currentUserId = SecurityUtils.getCurrentUserId();
        if (!projectMemberService.isProjectMember(stage.getProjectId(), currentUserId)) {
            return Result.fail("您不是该项目的成员");
        }

        return Result.success(stage);
    }

    /**
     * 获取项目所有阶段
     */
    @GetMapping("/list/{projectId}")
    public Result<List<SysProjectStage>> getProjectStages(@PathVariable Long projectId) {
        // 检查当前用户是否是项目成员
        Long currentUserId = SecurityUtils.getCurrentUserId();
        if (!projectMemberService.isProjectMember(projectId, currentUserId)) {
            return Result.fail("您不是该项目的成员");
        }

        List<SysProjectStage> stages = projectStageService.getProjectStages(projectId);
        return Result.success(stages);
    }

    /**
     * 分页查询项目阶段
     */
    @GetMapping("/page/{projectId}")
    public Result<Page<SysProjectStage>> pageProjectStages(
            @PathVariable Long projectId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        // 检查当前用户是否是项目成员
        Long currentUserId = SecurityUtils.getCurrentUserId();
        if (!projectMemberService.isProjectMember(projectId, currentUserId)) {
            return Result.fail("您不是该项目的成员");
        }

        Page<SysProjectStage> stagePage = projectStageService.listProjectStages(projectId, page, size);
        return Result.success(stagePage);
    }

    /**
     * 更新阶段完成率
     */
    @PutMapping("/completion/{stageId}")
    public Result<Void> updateStageCompletionRate(
            @PathVariable Long stageId,
            @RequestParam Integer completionRate) {
        // 获取阶段信息
        SysProjectStage stage = projectStageService.getStageById(stageId);

        // 检查当前用户是否有权限更新完成率
        Long currentUserId = SecurityUtils.getCurrentUserId();
        if (!projectMemberService.isProjectMember(stage.getProjectId(), currentUserId)) {
            return Result.fail("您不是该项目的成员");
        }

        projectStageService.updateStageCompletionRate(stageId, completionRate);
        return Result.success();
    }

    /**
     * 从模板创建项目阶段
     */
    @PostMapping("/create-from-template")
    public Result<Integer> createStagesFromTemplate(
            @RequestParam Long projectId,
            @RequestParam Long templateId) {
        // 检查当前用户是否有权限创建阶段
        Long currentUserId = SecurityUtils.getCurrentUserId();
        Integer role = projectMemberService.getMemberRole(projectId, currentUserId);
        if (role == null || role != 0) {
            return Result.fail("只有项目创建者才能从模板创建阶段");
        }

        int count = projectStageService.createStagesFromTemplate(projectId, templateId);
        return Result.success(count);
    }
}
